package com.taiji.dianthus.api.sys;


import com.taiji.dianthus.common.BasicController;
import com.taiji.dianthus.common.BusinessMessage;
import com.taiji.dianthus.domain.sys.DSysRole;
import com.taiji.dianthus.service.sys.RoleService;
import com.taiji.dianthus.service.sys.SysLogService;
import com.taiji.dianthus.util.IPUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import static com.taiji.dianthus.common.Constant.*;

/**
 * @ClassName SysRoleController
 * @Description
 * @Author H.M
 * @Date 2019/12/24
 */
@Api(value = "角色api")
@RestController
@RequestMapping(value = "/api/role")
public class SysRoleController extends BasicController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private SysLogService sysLogService;

    @RequestMapping(value = "/save")
    @ResponseBody
    @ApiOperation(value = "保存/修改角色方法", notes = "保存/修改角色接口")
    @ApiImplicitParam(name = "role", value = "保存/修改角色", required = true, dataType = "DSysRole")
    public BusinessMessage saveRole(HttpServletRequest request, @RequestBody DSysRole role) {
        BusinessMessage message = roleService.saveRole(role);

        if (StringUtils.isEmpty(role.getId())) {
            //保存操作日志
            sysLogService.save(currentUser(), LOG_OPERATION_SAVE, IPUtil.getIpAddr(request),
                    "新增角色[" + role.getRoleDesc() + "]", message.getSuccess() ? 1 : 0, message.getMsg());
        } else {
            sysLogService.save(currentUser(), LOG_OPERATION_UPDATE, IPUtil.getIpAddr(request),
                    "修改角色[" + role.getRoleDesc() + "]", message.getSuccess() ? 1 : 0, message.getMsg());
        }

        return message;

    }


    @RequestMapping(value = "/saveMenuRole")
    @ResponseBody
    @ApiOperation(value = "保存角色的菜单权限", notes = "保存角色的菜单权限接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuIds", value = "菜单主键,多个以,号隔开", dataType = "string", required = true, paramType = "query"),
            @ApiImplicitParam(name = "roleId", value = "角色ID", dataType = "string", required = true, paramType = "query")})
    public BusinessMessage saveRole(HttpServletRequest request, String menuIds, String roleId) {
        BusinessMessage message = roleService.saveOrUpdateRoleMenu(menuIds, roleId);
        DSysRole dSysRole = roleService.getRoleById(roleId);
        //保存操作日志
        sysLogService.save(currentUser(), LOG_OPERATION_UPDATE, IPUtil.getIpAddr(request),
                "更新角色[" + dSysRole.getRoleDesc() + "]菜单权限", message.getSuccess() ? 1 : 0, message.getMsg());
        return message;
    }


    @RequestMapping(value = "/delete")
    @ResponseBody
    @ApiOperation(value = "删除角色", notes = "删除角色接口")
    @ApiImplicitParam(name = "id", value = "主键", dataType = "string", required = true, paramType = "query")
    public BusinessMessage deleteRole(HttpServletRequest request, @Param("id") String id) {
        DSysRole dSysRole = roleService.getRoleById(id);
        BusinessMessage message = roleService.deleteRole(id);
        //保存操作日志
        sysLogService.save(currentUser(), LOG_OPERATION_DEL, IPUtil.getIpAddr(request),
                "删除角色[" + dSysRole.getRoleDesc() + "]", message.getSuccess() ? 1 : 0, message.getMsg());
        return message;
    }
}
